package com.qf.java2102.qihaonan.hotel.dao.impl;

import com.qf.java2102.qihaonan.hotel.dao.IFoodDao;
import com.qf.java2102.qihaonan.hotel.pojo.Food;
import com.qf.java2102.qihaonan.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qhn
 * @version 1.0
 */
public class FoodDaoImpl implements IFoodDao {

    private QueryRunner queryRunner = null;

    @Override
    public List<Food> findByCondition(String foodName, Long typeId, Integer currentPage, Integer pageSize) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());

        List<Object> params = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT\n" +
                                            "  `food_id` foodId,\n" +
                                            "  `type_id` typeId,\n" +
                                            "  `food_name` foodName,\n" +
                                            "  `food_price` foodPrice,\n" +
                                            "  `food_mprice` foodMprice,\n" +
                                            "  `food_image` foodImage,\n" +
                                            "  `food_desc` foodDesc\n" +
                                            "FROM\n" +
                                            "  t_food\n" +
                                            "WHERE 1=1 ");
        //查询条件
        //1.菜品名称   模糊查询
        if(!"".equals(foodName)) {
            sql.append(" AND food_name LIKE ? ");
            params.add(foodName);
        }
        //2.菜系ID     等值判断
        if(typeId.intValue() != 0) {
            sql.append(" AND type_id = ? ");
            params.add(typeId);
        }
        sql.append(" LIMIT ?,?");
        params.add((currentPage-1) * pageSize);
        params.add(pageSize);
        // foodName, (currentPage-1) * pageSize, pageSize
        return queryRunner.query(JdbcUtils.getConnection(), sql.toString(), new BeanListHandler<>(Food.class), params.toArray());
    }

    @Override
    public Long countByCondition(String foodName, Long typeId) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        List<Object> params = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT count(1) FROM t_food WHERE 1=1 ");
        //查询条件
        //1.菜品名称   模糊查询
        if(!"".equals(foodName)) {
            sql.append(" AND food_name LIKE ? ");
            params.add(foodName);
        }
        //2.菜系ID     等值判断
        if(typeId.intValue() != 0) {
            sql.append(" AND type_id = ? ");
            params.add(typeId);
        }
        return queryRunner.query(JdbcUtils.getConnection(), sql.toString(), new ScalarHandler<>(), params.toArray());
    }

    @Override
    public Food findById(Long foodId) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  `food_id` foodId,\n" +
                "  `type_id` typeId,\n" +
                "  `food_name` foodName,\n" +
                "  `food_price` foodPrice,\n" +
                "  `food_mprice` foodMprice,\n" +
                "  `food_image` foodImage,\n" +
                "  `food_desc` foodDesc\n" +
                "FROM\n" +
                "  t_food\n" +
                "WHERE food_id = ?";
        return queryRunner.query(JdbcUtils.getConnection(), sql, new BeanHandler<>(Food.class), foodId);
    }
}
